#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 发行筹资
爬取类目：发行筹资(不包含: 招股说明书、上市公告书、配股说明书)
股票类型不包括港股（港股没有发行筹资的资料）
url_sample: http://www.cninfo.com.cn/information/issue/szmb000001.html
"""

import sys
import json
from bs4 import BeautifulSoup
from xtls.logger import get_logger

from company_crawler import CompanyCrawler


reload(sys)
sys.setdefaultencoding('utf-8')

"""
result_sample_of_company_issue = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'issue': {
        'issueType',
        'issueStartDate',
        'issueProperty',
        'issueStockType',
        'releaseMode',
        'issuedPublicShares',
        'issuedPriceRmb',
        'issuedPriceForeign',
        'issueFundRaised',
        'issueCost',
        'pe',
        'expectedRatioOl',
        'expectedRatioSm',
    }
}
"""

def get_detail_mapping():
    DETAIL_MAPPING = {
        u'发行类型：': 'issueType',
        u'发行起始日：': 'issueStartDate',
        u'发行性质：': 'issueProperty',
        u'发行股票种类：': 'issueStockType',
        u'发行方式：': 'releaseMode',
        u'发行公众股数量(万股)：': 'issuedPublicShares',
        u'人民币发行价格(元)：': 'issuedPriceRmb',
        u'外币发行价格(元)：': 'issuedPriceForeign',
        u'实际募集资金(万元)：': 'issueFundRaised',
        u'实际发行费用(万元)：': 'issueCost',
        u'发行市盈率(倍)：': 'pe',
        u'上网定价中签率(%)：': 'expectedRatioOl',
        u'二级配售中签率(%)：': 'expectedRatioSm',
    }
    return DETAIL_MAPPING

class CompanyIssue(CompanyCrawler):
    def __init__(self, stock_list):
        logger = get_logger(__file__)
        super(CompanyIssue, self).__init__(stock_list=stock_list, logger=logger)
        self.category = 'issue'
        self.tab_name = 'issue'
        self.detail_mapping = get_detail_mapping()

    def find_company_info_from(self, url):
        result = {}
        html = self.get(url)

        if not html:
            return result

        soup = BeautifulSoup(html, 'html5lib')
        if not soup.find('img', src='/error.jpg'):
            result.update(self._find_issue_table(soup.select('div.clear > table')[0]))
        else:
            self.logger.info(u'undealed: not find the company %s' % url)
        return result

    def _find_issue_table(self, soup):
        issue_table = {}
        for tr in soup.find_all('tr'):
            tds = tr.find_all('td')
            if len(tds) != 2:
                continue
            title = tds[0].getText()
            value = tds[1].getText()

            key = self.detail_mapping.get(title, None)
            if key:
                issue_table[key] = value
            else:
                self.logger.info(u'undefined %s' % title)

        return {'issue': issue_table}

    def clean_data(self, dirty_data):
        self.cleaner.set_dirty_data(dirty_data)
        self.cleaner.clean_ten_thousand_to_int(['issuedPublicShares', 'issueFundRaised', 'issueCost'])
        self.cleaner.clean_percent_to_float(['expectedRatioOl', 'expectedRatioSm'])
        self.cleaner.clean_str_to_float(['issuedPriceRmb', 'issuedPriceForeign'])
        self.cleaner.clean_get_id_single(['issueType', 'issueProperty', 'issueStockType'], conn=self.conn)
        self.cleaner.clean_get_id_list(['releaseMode'], conn=self.conn)
        return self.cleaner.complete()

    def save_update(self, data):
        single = data.get('issue', None)
        if not single:
            self.logger.warn('issue has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return
        if not data.get('stock_id'):
            return

        single_clean = self.clean_data(single)
        single_clean['stockId'] = data.get('stock_id', None)
        single_clean['versionNum'] = 1 + self.select_current_version_num(table_name='issue',
                                                                         stock_id=data.get('stock_id'))
        self.insert_to_db(table_name='issue', cleaned_data=single_clean)

    def save_print(self, data):
        print json.dumps(data, indent=4)
        print '='*70

def main():
    pass

if __name__ == '__main__':
    main()

